﻿using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;
using System.Web.UI;

namespace ud_gSDMS.timesheet
{
    public partial class approve : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        protected string _pageId;
        private SiteUser siteUser;
        private int currentUserID = 0;
        void Page_Load(object sender, EventArgs e)
        {
            if (SiteUtils.GetCurrentSiteUser()==null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                siteUser = SiteUtils.GetCurrentSiteUser();
                currentUserID = siteUser.UserId;
                _pageId = pageId.ToString();
                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }
            }
        }
        private void PopulateLabels()
        {
            
        }
        private void PopulateControls()
        {
            datTimeSheet_Date_from.SelectedDate = DateTime.Now.AddDays(-14);
            datTimeSheet_Date_to.SelectedDate = DateTime.Now;
            datTimeSheet_Date_from.MaxDate = DateTime.Now;
            datTimeSheet_Date_to.MaxDate = DateTime.Now;
            //cboWorkStatus.SelectedValue = "1";
            LoadControl();
            LoadGrid();
        }
        private void LoadGrid()
        {
            if(cboWorkStatus.SelectedValue =="1")
            {
                cboWorkStatus.Text = "Open";
            }
            //cboAccount.Text.Trim() == "" ? null : cboAccount.SelectedValue,
            var data = gSDMS_BL.ud_gSDMS_TIMESHEET_SearchApprove(datTimeSheet_Date_from.SelectedDate == null ? null : datTimeSheet_Date_from.SelectedDate.Value.ToShortDateString(),
                                                         datTimeSheet_Date_to.SelectedDate == null ? null : datTimeSheet_Date_to.SelectedDate.Value.ToShortDateString(),
                                                         cbProjectCode.Text.Trim() == "" ? null : cbProjectCode.SelectedValue,
                                                         cboLeader.SelectedValue,
                                                         cboAccount.Text.Trim() == "" ? null : cboAccount.SelectedValue,
                                                         cboWorkStatus.Text.Trim() == "" ? null : cboWorkStatus.SelectedValue, currentUserID.ToString());

            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }

            grdTimeSheet.DataSource = data;
            grdTimeSheet.DataBind();
        }
        private void LoadControl()
        {
            var db = new gSDMS_DataLinq();
            DataTable tb1 = Library.LINQToDataTable(db, db.ud_gSDMS_CAT_TIMESHEET_STATUS);
            cboWorkStatus.Items.Add(new RadComboBoxItem("",""));
            for (int i = 0; i < tb1.Rows.Count; i++)
            {
                cboWorkStatus.Items.Add(new RadComboBoxItem(tb1.Rows[i]["Name"].ToString(), tb1.Rows[i]["CAT_TimeSheet_Status_ID"].ToString()));
            }
            //cboWorkStatus.DataValueField = "CAT_TimeSheet_Status_ID";
            //cboWorkStatus.DataTextField = "Name";
            //cboWorkStatus.DataSource = Library.LINQToDataTable(db, db.ud_gSDMS_CAT_TIMESHEET_STATUS);
            //cboStatus.DataSource = gSDMS_BL.ud_gSDMS_TIMESHEET_Search(null, null, null, null);
            //cboWorkStatus.DataBind();

            var tb = gSDMS_BL.getComboProjectCode(currentUserID);
            cbProjectCode.Items.Add(new RadComboBoxItem("",""));
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                 cbProjectCode.Items.Add(new RadComboBoxItem(tb.Rows[i]["Code"].ToString(),tb.Rows[i]["Project_ID"].ToString()));
            }
            //cbProjectCode.DataSource = tb;
            //cbProjectCode.DataValueField = "Project_ID";
            //cbProjectCode.DataTextField = "Code";
            //cbProjectCode.DataBind();
            cboWorkStatus.SelectedIndex = 1;

            cboLeader.DataSource = gSDMS_BL.ud_gSDMS_PROJECT_Leader_Tab();
            cboLeader.DataValueField = "UserID";
            cboLeader.DataTextField = "LoginName";
            cboLeader.DataBind();
            cboLeader.Items.Insert(0, new RadComboBoxItem("", ""));
            cboLeader.SelectedValue = "";
        }
        public void itemSelected(object sender, EventArgs e)
        {
            //Response.Write("<script>alert('Hello')</script>");
            if (string.IsNullOrEmpty(cbProjectCode.Text)==true)
            {
                cboAccount.Items.Clear();
                cboAccount.Text = "";
                return;
            }
            else
            {
                cboAccount.Items.Clear();
                DataTable tb = gSDMS_BL.getListAccountID(cbProjectCode.Items[cbProjectCode.SelectedIndex].Value);
                cboAccount.Items.Add(new RadComboBoxItem("", ""));
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    cboAccount.Items.Add(new RadComboBoxItem(tb.Rows[i]["LoginName"].ToString(), tb.Rows[i]["UserID"].ToString()));
                }
                
            }
               
        }

        private void grdTimeSheet_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdTimeSheet.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        private void grdTimeSheet_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "delete":
                    gSDMS_BL.ud_gSDMS_TIMESHEET_Del(e.CommandArgument.ToString(), true);
                    break;
                case "detail":
                    WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/timesheet/apptimesheet.aspx?&id=" + e.CommandArgument.ToString());
                    break;
            }
        }
        void grdTimeSheet_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }
        void grdTimeSheet_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            grdTimeSheet.EditIndex = -1;
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;

            LoadGrid();
        }

        void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            LoadGrid();
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdTimeSheet.PageIndexChanging += grdTimeSheet_PageIndexChanging;
            grdTimeSheet.RowCommand += grdTimeSheet_RowCommand;
            grdTimeSheet.RowDeleting += grdTimeSheet_RowDeleting;
            grdTimeSheet.Sorting += grdTimeSheet_Sorting;
            grdTimeSheet.RowDataBound += new GridViewRowEventHandler(grdTimeSheet_RowDataBound);
            btnSearch.Click += btnSearch_Click;

            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        void grdTimeSheet_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                int TimeSheet_ID = (int)DataBinder.Eval(e.Row.DataItem, "TimeSheet_ID");
                string status = (string)DataBinder.Eval(e.Row.DataItem, "TimeSheet_Status");
                if (status.Trim().Equals("Approved") == false)
                {
                    e.Row.FindControl("btn_Edit").Visible = true;
                }
            }
        }

        //void btnFinish_Click(object sender, EventArgs e)
        //{
        //    WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
        //}

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}